package com.atguigu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.mapper.BaseCategoryViewMapper;
import com.atguigu.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author zhangqiang
 * @since 2021-11-01
 */
@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    @Override
    public List<JSONObject> getIndexCategoryInfo() {

        //1.查询所有的分类信息
        List<BaseCategoryView> viewList = baseMapper.selectList(null);
        ArrayList<JSONObject> categoryListJSON = new ArrayList<>();
        //2.根据id进行分类：一级分类
        Map<Long, List<BaseCategoryView>> category1Map = viewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //迭代
        Set<Map.Entry<Long, List<BaseCategoryView>>> category1Entries = category1Map.entrySet();
        int index=1;
        for (Map.Entry<Long, List<BaseCategoryView>> category1Entry : category1Entries) {

            Long category1Id = category1Entry.getKey();
            List<BaseCategoryView> category1List = category1Entry.getValue();
            //声明一个JSONObject 将一级分类数据存储上
            JSONObject category1 = new JSONObject();
            category1.put("index",index++);
            category1.put("categoryId",category1Id);
            category1.put("categoryName",category1List.get(0).getCategory1Name());

            //3.根据id进行分类：二级分类
            Map<Long, List<BaseCategoryView>> category2Map = category1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //声明一个集合来存储一级分类下所有的二级分类数据
            ArrayList<JSONObject> category1Children = new ArrayList<>();
//循环遍历category2Map 获取二级分类Id，二级分类名称
            Set<Map.Entry<Long, List<BaseCategoryView>>> categoryEntries = category2Map.entrySet();
            for (Map.Entry<Long, List<BaseCategoryView>> categoryEntry : categoryEntries) {
                Long category2Id = categoryEntry.getKey();
                List<BaseCategoryView> category2List = categoryEntry.getValue();
                JSONObject category2 = new JSONObject();
                category2.put("categoryId", category2Id);
                category2.put("categoryName",category2List.get(0).getCategory2Name());

                //4.根据id进行分类：三级级分类
                Map<Long, List<BaseCategoryView>> category3Map = category2List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
                ArrayList<JSONObject> category2Children = new ArrayList<>();
                Set<Map.Entry<Long, List<BaseCategoryView>>> entries = category3Map.entrySet();
                for (Map.Entry<Long, List<BaseCategoryView>> entry : entries) {
                    Long category3Id = entry.getKey();
                    List<BaseCategoryView> category3List = entry.getValue();

                    JSONObject category3 = new JSONObject();
                    category3.put("categoryId",category3Id);
                    category3.put("categoryName",category3List.get(0).getCategory3Name());
                    category2Children.add(category3);
                }


                category2.put("categoryChild",category2Children);
                //讲二级分类放到category2Child集合里面
                category1Children.add(category2);
            }

            category1.put("categoryChild",category1Children);
            categoryListJSON.add(category1);

            }


        return categoryListJSON;
    }
}
